package com.wentao.mall.api

import com.wentao.mall.dao.InvoiceDao
import com.wentao.mall.entity.PageQuery
import com.wentao.mall.entity.ProTableResult
import com.wentao.mall.entity.simple
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RestController
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

@RestController
class InvoiceController(val invoiceDao: InvoiceDao) {
    @GetMapping("/admin/invoice/list")
    fun list(pageQuery: PageQuery): ProTableResult {
        val page = invoiceDao.findAll(pageQuery.toPageRequest())
        return page.simple()
    }

    @PostMapping("/admin/invoice/save")
    fun save(invoice: Invoice): Result {
        invoiceDao.save(invoice)
        return Result.success()
    }

    @PostMapping("/admin/invoice/delete")
    fun delete(id: Int): Result {
        invoiceDao.deleteById(id)
        return Result.success()
    }
}

@Entity
data class Invoice(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Int?,
    val url: String?
)